(function($) {

	$(function() {
		var oTable = $('#datatable').dataTable(
				{
					bProcessing : true,
					"sAjaxSource" : window.basePath + "/art/article_data_"+ global.type + ".html",
					"fnServerParams" : function(aoData) {
						var search = $('#searchForm')._formToJson();
						for ( var i in search) {
							aoData.push({
								"name" : i,
								"value" : search[i]
							});
						}
					},
					"aoColumnDefs" : [ {
						"aTargets" : [ 0 ],
						"width" : "8%",
						"mData" : "title",
						"mRender" : function(data, type, full) {
							return data;
						}
					}, {
						"aTargets" : [ 1 ],
						"width" : "8%",
						"mData" : "author",
						"mRender" : function(data, type, full) {
							return data;
						}
					}, {
						"aTargets" : [ 2 ],
						"width" : "8%",
						"mData" : "date",
						"mRender" : function(data, type, full) {
							return data;
						}
					}, {
						"aTargets" : [ 3 ],
						"width" : "8%",
						"mData" : "recommend",
						"mRender" : function(data, type, full) {
							return setRecommendHtml(data);
						}
					}, {
						"aTargets" : [ 4 ],
						"width" : "8%",
						"mData" : "spaceShow",
						"mRender" : function(data, type, full) {
							return setShowHtml(data);
						}
					}, {
						"aTargets" : [ 5 ],
						"width" : "8%",
						"mData" : "id",
						"mRender" : function(data, type, full) {
							var html = '<div class="btn-group">';
							html += '<a class="btn btn-info btn-xs upd" target="_blank" href="'
									+window.basePath+'/art/edit_'+global.type+'.html?id='+data+'"><i class="fa fa-edit"></i> 编辑</a>';
							html += '<a class="btn btn-danger btn-xs del" target="_blank"><i class="fa fa-times"></i> 删除</a>';
							html += "</div>";
							return html;
						}
					} ]
				}).on('click', 'a.recombtn', function() {/* 推荐 */
					var $this = $(this);
					var tr = $this.parents('tr:first');
					var aData = oTable.fnGetData(tr[0]);
					var status = aData.recommend;
					var tip = "确认推荐此文章吗？";
					if (status == 1) {
						tip = "确认取消推荐此文章吗？";
					}
					$.modalConfirm({
		                id:'modal-confirm',
		                content:tip,
		                ok:function(){
		                	$.post("/admin/art/updrm.html", {
								id : aData.id,
								recommend : status
							}, function(r) {
								asyncbox.tips(r.m, r.r == 0 ? 'success' : 'error');
								if (r.r == 0) {
									$this.parent("td").html(setRecommendHtml(r.d));
									oTable.fnGetData(tr[0]).recommend = r.d;
								}
							}, "json");
		                }
		            });
				}).on('click', 'a.showbtn', function() {/* show */
					var $this = $(this);
					var tr = $this.parents('tr:first');
					var aData = oTable.fnGetData(tr[0]);
					var status = aData.spaceShow;
					var tip = "确认显示此文章吗？";
					if (status == 1) {
						tip = "确认取消显示此文章吗？";
					}
					$.modalConfirm({
		                id:'modal-confirm',
		                content:tip,
		                ok:function(){
		                	$.post("/admin/art/updsp.html", {
								id : aData.id,
								spaceShow : status
							}, function(r) {
								asyncbox.tips(r.m, r.r == 0 ? 'success' : 'error');
								if (r.r == 0) {
									$this.parent("td").html(setShowHtml(r.d));
									oTable.fnGetData(tr[0]).spaceShow = r.d;
								}
							}, "json");
		                }
		            });
				}).on('click', 'a.del', function() {/*del*/
					var $this = $(this);
					var tr = $this.parents('tr:first');
					var aData = oTable.fnGetData(tr[0]);
					var status = aData.spaceShow;
					$.modalConfirm({
		                id:'modal-confirm',
		                content:"确认删除此文章吗？<br/>[ 标题："+aData.title+" ]",
		                ok:function(){
		                	$.post("/admin/art/del_"+aData.id+".html", {}, function(r) {
								asyncbox.tips(r.m, r.r == 0 ? 'success' : 'error');
								if (r.r == 0) {
									fileFunc.delById(aData.file);
									reload();
								}
							}, "json");
		                }
		            });
				});

		/**
		 * 查询按钮点击事件
		 */
		$("#searchButton").click(function() {
			reload();
		});
		var setRecommendHtml = function(data) {
			var html = '<a class="btn btn-success btn-xs recombtn" href="javascript:void(0);"><i class="fa fa-check-circle"></i> 推荐</a>';
			// 0:不推荐 1推荐
			if (data == 1) {
				html = '<a class="btn btn-danger btn-xs recombtn" href="javascript:void(0);"><i class="fa fa-times-circle"></i> 取消推荐</a>';
			}
			return html;
		};
		var setShowHtml = function(data) {
			// 0:不显示 1显示
			var html = '<a class="btn btn-danger btn-xs showbtn" href="javascript:void(0);"><i class="fa fa-times-circle"></i> 取消显示</a>&nbsp;';
			if (data == 0) {
				html = '<a class="btn btn-success btn-xs showbtn" href="javascript:void(0);"><i class="fa fa-check-circle"></i> 显示</a>&nbsp;';
			}
			return html;
		};
	});
})(jQuery);
var reload = function() {
	$('#datatable').dataTable()._fnAjaxReload();
};